﻿using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using T.ApiResults;
using T.Vehicleservice.API.Application.Command.ExpenseCommand;
using T.Vehicleservice.API.Application.Command.MainCommand;
using T.Vehicleservice.API.Application.Command.MaintenanceRecordCommand;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;

namespace T.Vehicleservice.API.Controllers
{
    /// <summary>
    /// 费用
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ExpenseController : ControllerBase
    {
        IMediator mediator;

        public ExpenseController(IMediator mediator)
        {
            this.mediator = mediator;
        }
        /// <summary>
        /// 添加费用信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> CreateExprenRecord(ExpenseCreateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                command.CompanyID = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司Id,
                command.User_Id = int.Parse(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value); //获取用户Id,
                command.CreatedBy = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取名称
                command.CreatedDate = DateTime.Now;
              result.Data=await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 删除费用信息（批删）
        /// </summary>
        /// <param name="Command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DeleteAllExpensoRecord(ExpenseDeleteCommand Command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                Command.DeletedBy = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取名称
                Command.DeletedDate = DateTime.Now;
                result.Data = await mediator.Send(Command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 修改费用信息
        /// </summary>
        /// <param name="MaintenanceRecordUpdateCommand"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdateExpenenanceRecord(ExpenseUpdateCommand Command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                Command.CompanyID = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司Id,
                Command.User_Id = int.Parse(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value); //获取用户Id,
                Command.UpdatedBy = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取名称
                Command.UpdatedDate = DateTime.Now;
                result.Data = await mediator.Send(Command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;

        }

    }
}
